<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>
        //创建一个对象(属性：品牌、价格、时速、方法：跑一定距离所花的事件)
        var car = {
            brand: '宝马',
            price: 3000,
            speed: 30000,
            costTime(distance) {
                return distance / this.speed;
            }
        }

        console.log(car)

        // 定义一个圆求它的距离
        var point = {
            x: 3,
            y: 4,
            distance1() {
                return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));

            }
        }

        //设置一个矩形的构造函数
        function Rect(width, height) {
            //这个this代表的是创建这个对象
            this.width = width;
            this.height = height;
            this.area = function() {
                //这个this代表的是调用这个函数的实例

                return this.width * this.height;
            }
        }
        var p = new Rect(10, 30)
        p.area()
        console.log(p);

        //
        function Car(brand, price, speed) {
            this.brand = brand;
            this.price = price;
            this.speed = speed;

            this.coatTiem = function(road) {
                return road.length / this.speed;

            }

        }

        function Road(name, length) {
            this.name = name;
            this.length = length;

        }

        var ca = new Car('宝马', '40000', '300')
        var Ro = new Road('京广线', 500)
        console.log(ca);
        console.log(Ro);
        console.log(ca.coatTiem(Ro))

        //给出一般书 求他的出版到今天的日期
        function Book(name, author, publisher, publishTime) {
            this.name = name;
            this.author = author;
            this.publisher = publisher;
            this.publishTime = publishTime;

            this.showInfo = function() {
                alert(`书名:<<${this.name}>>\n作者${this.author}\n 出版社:${this.publisher}\n出版日期${this.publishTime}`);


            }

            this.days = function() {
                var date = new Date();
                var date2 = new Date(this.punlishTime)
                var num = (date - date2) / 1000 / 60 / 60 / 24;
                alert(`这本书出版了${Math.floor(num)}天`)
            }
        }

        var book1 = new Book('三国演义', '罗贯中', '人民', '2002-2-2');
    </script>
</body>

</html>